// Karakter.cpp : Implementation of CKarakter
#include "stdafx.h"
#include "EBASdll.h"
#include "Karakter.h"

/////////////////////////////////////////////////////////////////////////////
// CKarakter

STDMETHODIMP CKarakter::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IKarakter
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CKarakter::get_GUID(BSTR *pVal)
{
	*pVal = m_guid.copy();

	return S_OK;
}

STDMETHODIMP CKarakter::get_Karakter(BSTR *pVal)
{
	*pVal = m_karakter.copy();

	return S_OK;
}

STDMETHODIMP CKarakter::put_Karakter(BSTR newVal)
{
	m_karakter = newVal;

	return S_OK;
}

STDMETHODIMP CKarakter::get_KarakterSkala(IKarakterSkala **pVal)
{
	if (m_pKarakterSkala) {
		*pVal = m_pKarakterSkala;
		(*pVal)->AddRef();
	} else
		*pVal = NULL;

	return S_OK;
}

STDMETHODIMP CKarakter::put_KarakterSkala(IKarakterSkala *newVal)
{
	m_pKarakterSkala = newVal;
	if (m_pKarakterSkala)
		m_pKarakterSkala->AddRef();

	return S_OK;
}

STDMETHODIMP CKarakter::SetInfo()
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_GEM_KARAKTER";

	try {
		BSTR	pSkala = bstr_t("");
		bstr_t	skala = bstr_t("");
				
		if (NULL == m_pKarakterSkala)
			return E_INVALIDARG;

		m_pKarakterSkala->get_Navn(&pSkala);
		skala = pSkala;

		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("karakter", adVarChar, adParamInput, 255, m_karakter) );
		command->Parameters->Append( command->CreateParameter("karakterSkala", adVarChar, adParamInput, 255, skala) );
		
		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = (bstr_t)recordset->Fields->GetItem("GUID")->Value;
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}

STDMETHODIMP CKarakter::Slet()
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_SLET_KARAKTER";

	try {
		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("karakterGuid", adVarChar, adParamInput, 255, m_guid) );
		
		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = bstr_t("");
		m_karakter = bstr_t("");
		m_pKarakterSkala = NULL;

		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}
